<%= javascript_include_tag 'json2' %>
<%= javascript_include_tag 'map' %>
<script type="text/javascript">
  <!--
  // Global variables
  var map;
  var MAX_X = 800, MAX_Y = 640;

  window.onload = function () {
    var canvas = $('canvas');
    var map_o_x = $('map_o_x'), map_o_y = $('map_o_y');
    map = new Map('<%= compute_public_path(@map.image.url, '') %>',
        canvas,
        MAX_X,
        MAX_Y,
        <%= @map.o_x %>, <%= @map.o_y %>,
        map_o_x, map_o_y);
    map.draw();
    // Draw guide lines for mouse position
    canvas.observe('mousemove', map.drawWithLine.bind(map));
    canvas.observe('mouseout', map.drawWithoutLine.bind(map));
    // Update current position by mouse click
    canvas.observe('click', map.setCurrentPosition.bind(map));

    // Redraw current position when curr_x, curr_y values are updated
    map_o_x.observe('change', map.changeValueX.bind(map));
    map_o_y.observe('change', map.changeValueY.bind(map));
  };
-->
</script>
<canvas id="canvas" width="500" height="300"></canvas>
<div id="message"></div>
<div id="form">
<%= form_for(@map, :html => { :multipart => true}) do |f| %>
  <% if @map.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@map.errors.count, "error") %> prohibited this map from being saved:</h2>

      <ul>
      <% @map.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :o_lat %><br />
    <%= f.text_field :o_lat %>
  </div>
  <div class="field">
    <%= f.label :o_lon %><br />
    <%= f.text_field :o_lon %>
  </div>
  <div class="field">
    <%= f.label :o_alt %><br />
    <%= f.text_field :o_alt %>
  </div>
  <div class="field">
    <%= label_tag :gpsd_tpv %><br />
    <%= text_area_tag :gpsd_tpv %><br />
    <%= check_box_tag :use_gpsd_tpv %>Use GPSD TPV to set position<br />
  </div>
  <div class="field">
    <%= f.label :brng %><br />
    <%= f.text_field :brng %>
  </div>
  <div class="field">
    <%= f.label :o_x %><br />
    <%= f.text_field :o_x %>
  </div>
  <div class="field">
    <%= f.label :o_y %><br />
    <%= f.text_field :o_y %>
  </div>
  <div class="field">
    <%= f.label :dist_pix_ratio %><br />
    <%= f.text_field :dist_pix_ratio %>
  </div>
  <div class="field">
    <%= f.label :image %><br />
    <%= f.file_field :image %>
  </div>
  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_field :description %>
  </div>
  <div class="field">
    <%= f.label :basis_map_relation %><br />
    <%- @maps.each do |map| -%>
      <%- next if @map == map -%>
    <%= radio_button_tag 'basis_map_relation[id]', map.id, (@map.basis_map == map) %><%= map.name %><br />
    <%- end -%>
    <%= radio_button_tag 'basis_map_relation[id]', -1, @map.basis_map.nil? %>Use absolute position<br />
    <table>
      <tr>
        <td>Altitude difference</td>
        <td><%= text_field_tag('basis_map_relation[alt_diff]', @map.basis_map_relation.nil? ? '' : @map.basis_map_relation.alt_diff, :size => 10) %></td>
      </tr>
      <tr>
        <td>Bearing difference</td>
        <td><%= text_field_tag('basis_map_relation[brng_diff]', @map.basis_map_relation.nil? ? '' : @map.basis_map_relation.brng_diff, :size => 10) %></td>
      </tr>
      <tr>
        <td>Reference x-coordinate</td>
        <td><%= text_field_tag('basis_map_relation[ref_x]', @map.basis_map_relation.nil? ? '' : @map.basis_map_relation.ref_x, :size => 10) %></td>
      </tr>
      <tr>
        <td>Reference y-coordinate</td>
        <td><%= text_field_tag('basis_map_relation[ref_y]', @map.basis_map_relation.nil? ? '' : @map.basis_map_relation.ref_y, :size => 10) %></td>
      </tr>
    </table>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
</div>
